import axios from 'axios';
import { getTmpCache } from '@u/cache.js';

const whiteList = [
  '/api/userSystem/common/captcha',
  '/api/userSystem/login',
];

axios.interceptors.request.use(config => {
  const token = getTmpCache('token');
  if (token && !whiteList.includes(config.url)) { // 每次发送请求之前判断是否存在token，如果存在，则统一在http请求的header都加上token，不用每次请求都手动添加了
    config.headers['Cache-Control'] = 'no-cache';
    config.headers.Pragma = 'no-cache';
    config.headers.Authorization = token;
  }
  return config;
}, err => {
  return Promise.reject(err);
});

axios.interceptors.response.use(response => {
  return response;
}, error => {
  return Promise.reject(error);
});

export default axios;
